package com.smile.stream.exercises;

import com.smile.stream.pojo.Person;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.maxBy;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/8 16:41
 */


public class Demo2 {
    public static final List<Person> personList=new ArrayList<>(10);
    static{
        initData();
    }

    /**
     * 把集合 转换成一个map。key为账号，value为对象本身（后来者居上）
     * 把集合 转换成一个map。key为账号，value为name (key冲突设置value为“，”拼接字符串)
     * 把集合按照sex 分组
     * 把集合按照age分组，value为 name字段的集合
     * 把集合按照age分组，value为 name字段 以“，”拼接的字符串
     * 筛选集合中的女性，并按照年龄排序
     * 求出集合中最大的年龄
     * 求出集合中最小的年龄
     * 求出集合中平均年龄
     * 找出集合中 任意一个18岁的女生
     * 集合根据sex分组，并过滤年级最小的person
     */

    public static void main(String[] args) {
        function1();
        function2();
        function3();
        function4();
        function5();
        function6();
        function7();
        function8();
        function9();
        function10();
        function11();
    }

    private static void function1() {
        System.out.println("function1");
        Map<String, Person> collect = personList
                .stream()
                .collect(Collectors.toMap(Person::getAccount,Function.identity(),(v1,v2) -> v2));
        collect.entrySet().forEach(System.out::println);
    }

    private static void function2() {
        System.out.println("function2");
        Map<String, String> collect = personList
                .stream()
                .collect(Collectors.toMap(Person::getAccount,Person::getName,(v1,v2) -> v1+","+v2));
        collect.entrySet().forEach(System.out::println);
    }

    private static void function3() {
        System.out.println("function3");
        Map<Integer, List<Person>> collect = personList
                .stream()
                .collect(Collectors.groupingBy(Person::getSex));
        collect.entrySet().forEach(System.out::println);
    }

    private static void function4() {
        System.out.println("function4");
        Map<Integer, List<String>> collect = personList
                .stream()
                .collect(
                        Collectors.groupingBy(
                                Person::getSex,Collectors.mapping(Person::getName,Collectors.toList())
                        )
                );
        collect.entrySet().forEach(System.out::println);
    }

    private static void function5() {
        System.out.println("function5");
        Map<Integer, String> collect = personList
                .stream()
                .collect(
                        Collectors.groupingBy(
                                Person::getSex,Collectors.mapping(Person::getName,Collectors.joining(","))
                        )
                );
        collect.entrySet().forEach(System.out::println);
    }

    private static void function6(){
        System.out.println("function6");
        List<Person> collect = personList
                .stream()
                .filter(v -> Objects.equals(1,v.getSex()))
                .sorted(Comparator.comparing(Person::getAge))
                .collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
    private static void function7(){
        System.out.println("function7");
        Optional<Person> max = personList
                .stream()
                .max(Comparator.comparing(Person::getAge));
        System.out.println("max = " + max);
    }
    private static void function8(){
        System.out.println("function8");
        Optional<Person> min = personList
                .stream()
                .min(Comparator.comparing(Person::getAge));
        System.out.println("min = " + min);
    }

    private static void function9(){
        System.out.println("function9");
        Double average = personList
                .stream()
                .collect(Collectors.averagingInt(Person::getAge));
        System.out.println("average = " + average);
    }

    private static void function10(){
        System.out.println("function10");
        Optional<Person> personOptional = personList
                .stream()
                .filter(v -> Objects.equals(18,v.getAge()))
                .findAny();
        System.out.println("personOptional = " + personOptional);
    }

    private static void function11(){
        System.out.println("function11_1");
        Map<Integer, Person> collect = personList
                .stream()
                .collect(
                        Collectors.groupingBy(Person::getSex,
                                Collectors.collectingAndThen(
                                        maxBy(Comparator.comparingInt(Person::getAge)),Optional::get)
                        )
                );
        collect.entrySet().forEach(System.out::println);

    }

    private static void initData() {
        personList.add(new Person(1,"10001","小明",23,0));
        personList.add(new Person(2,"10002","小光",26,0));
        personList.add(new Person(3,"10003","小华",25,0));
        personList.add(new Person(4,"10003","花花",24,1));
        personList.add(new Person(5,"10006","小红",18,1));
        personList.add(new Person(6,"10007","小兰",17,1));

    }
}
